﻿using Common.Enum;
using DryWCS.Model.Enum;
using DryWCS.Model.Stacker;
using DryWCS.Service.DbContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DryWCS.Service.Stacker
{
    public class StackerTaskService : LocalDbContext<StackerTaskEntity>
    {
        public bool IsSpare()
        {
            try
            {
                return !Db.Queryable<StackerTaskEntity>().Where(p => p.Status == TaskStatusEnum.执行中 || p.Status == TaskStatusEnum.未执行 || p.Status == TaskStatusEnum.异常).Any();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public bool IsEmpty()
        {
            try
            {
                return !Db.Queryable<StackerTaskEntity>().Any();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public StackerTaskEntity GetFirstByStatus(TaskStatusEnum status, bool isloack = false)
        {
            try
            {
                return Db.Queryable<StackerTaskEntity>().Where(p => p.Status == status && p.IsLock == isloack).First();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public StackerTaskEntity GetFirst()
        {
            try
            {
                return Db.Queryable<StackerTaskEntity>().OrderBy (it=>it.Id,SqlSugar.OrderByType.Desc).First();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public StackerTaskEntity GetByWMSOrderID(int wmsOrderID)
        {
            try
            {
                return Db.Queryable<StackerTaskEntity>().Where(p => p.WMSOrderId == wmsOrderID).OrderBy(p => p.StartTime, SqlSugar.OrderByType.Desc).First();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
